module Tools::WebFetionsHelper
  
  def link_to_send_web_fetion_sm tool_or_setting
    link_to_function "发送短信", "
      if($F('verify_code') == '请输入右侧图片中的验证码 =>') {
        alert('请输入验证码 =>');
      } else { #{
      remote_function(
        :url => send_sm_tools_web_fetions_url,
        :method => :post,
        :with => "$('#{tool_or_setting.id}_web_fetion_form').serialize()",
        :loading => "$('web_fetion_send_sm_#{tool_or_setting.id}').innerHTML='发送中...'",
        :failure => "
          if(request.responseText == 'SSI_NEED_VERIFY') {
            alert('密码错误次数过多，需要验证');
          }
          if(request.responseText == 'SSI_NEED_VERIFY' || request.responseText == 'SSI_VERIFY_FAIL') {
            $('web_fetion_fetion_mobile').show();
            $('web_fetion_fetion_password').show();
            #{ remote_function( 
                :update => "fetion_verify_inputs",
                :url => verify_fetion_url(:default),
                :with => "'&mobile=' + $F('fetion_sm_#{tool_or_setting.id}_mobile')",
                :success => "$('fetion_verify').show()",
                :method => :get
              )}
          } else if( request.responseText == 'SSI_AUTH_FAIL' || request.responseText == 'cmcc_mobile_requested' || request.responseText == 'password_requested') {
            $('web_fetion_fetion_mobile').show();
            $('web_fetion_fetion_password').show();
            alert('发送失败，' + fetion_sm_state_name(request.responseText));
            $('web_fetion_send_sm_#{tool_or_setting.id}').innerHTML = '发送中...';
          } else if( request.responseText != 'SUCCESS') {
            alert('发送失败，' + fetion_sm_state_name(request.responseText));
            $('web_fetion_send_sm_#{tool_or_setting.id}').innerHTML = '发送中...';
          }
        ",
        :success => "
          alert(request.responseText);
          Try.these(function() {
            $('fetion_verify').hide();
            $('verify_code').value = '';  
            $('verify_image_id').value = '';  
          });                  
          #{ remote_function :url => check_fetion_changed_user_url(current_user),
               :method => :get,
               :with => "'&mobile='+ $F('fetion_sm_#{tool_or_setting.id}_mobile') + '&password=' + $F('fetion_sm_#{tool_or_setting.id}_password')"                       
           }
          ",
        :complete => "$('web_fetion_send_sm_#{tool_or_setting.id}').innerHTML='发送短信'"
      )}
    }",
    :class => :send_sm,
    :title => '发送短信',
    :id => "web_fetion_send_sm_#{tool_or_setting.id}"
  end
  
  def link_to_send_web_fetion_sm_in_show tool_or_setting
    link_to_function "发送短信", "
      if($F('verify_code') == '请输入右侧图片中的验证码 =>') {
        alert('请输入验证码 =>');
      } else { #{
      remote_function(
        :url => send_sm_tools_web_fetions_url,
        :method => :post,
        :with => "$('#{tool_or_setting.id}_web_fetion_form').serialize()",
        :loading => "$('web_fetion_send_sm_#{tool_or_setting.id}').innerHTML='发送中...'",
        :failure => "
          if(request.responseText == 'SSI_NEED_VERIFY') {
            alert('密码错误次数过多，需要验证');
          }
          if(request.responseText == 'SSI_NEED_VERIFY' || request.responseText == 'SSI_VERIFY_FAIL') {
            $('web_fetion_fetion_mobile').show();
            $('web_fetion_fetion_password').show();
            #{ remote_function( 
                :update => "fetion_verify_inputs",
                :url => verify_fetion_url(:default),
                :with => "'&mobile=' + $F('fetion_sm_#{tool_or_setting.id}_mobile')",
                :success => "$('fetion_verify').show()",
                :method => :get
              )}
          } else if( request.responseText == 'SSI_AUTH_FAIL' || request.responseText == 'cmcc_mobile_requested' || request.responseText == 'password_requested') {
            $('web_fetion_fetion_mobile').show();
            $('web_fetion_fetion_password').show();
            alert('发送失败，' + fetion_sm_state_name(request.responseText));
            $('web_fetion_send_sm_#{tool_or_setting.id}').innerHTML = '发送中...';
          } else if( request.responseText != 'SUCCESS') {
            alert('发送失败，' + fetion_sm_state_name(request.responseText));
            $('web_fetion_send_sm_#{tool_or_setting.id}').innerHTML = '发送中...';
          }
        ",
        :success => "        
          Try.these(function() {
            $('fetion_verify').hide();
            $('verify_code').value = '';  
            $('verify_image_id').value = '';  
          });alert('发送成功!'); "  + (has_logined_by_user? ? "
          #{ remote_function( :url => check_fetion_changed_user_url(current_user),
               :method => :get,
               :with => "'&mobile='+ $F('fetion_sm_#{tool_or_setting.id}_mobile') + '&password=' + $F('fetion_sm_#{tool_or_setting.id}_password')"
             ) 
           }          
          " : " 
          if(getValue('registry?') != false) {
            if(confirm('注册成为会员吗？这样子您就可以享受到更多的吐司服务了。')) { 
              window.location.href = '#{ new_user_url }';               
            } else {
              setValue('registry?',false);
            }
          }
        "),
        :complete => "$('web_fetion_send_sm_#{tool_or_setting.id}').innerHTML='发送短信'"
      )}
    }",
    :class => :send_sm,
    :title => '发送短信',
    :id => "web_fetion_send_sm_#{tool_or_setting.id}"
  end
  
  def link_to_update_fetion_friends tool_or_setting, args = {}
    link_to_function "获取好友", "
      if($F('verify_code') == '请输入右侧图片中的验证码 =>') {
        alert('请输入验证码 =>');
      } else { #{
      remote_function(
        :update => args[:update],
        :url => friends_tools_web_fetions_url,
        :method => :get,
        :with => "'&mobile=' + $F('fetion_sm_#{tool_or_setting.id}_mobile')  +'&password=' + $F('fetion_sm_#{tool_or_setting.id}_password') + '&verifyCode=' + $F('verify_code') + '&verifyImgId=' + $F('verify_image_id')  ",
        :loading => "$('fetion_friends_#{tool_or_setting.id}').innerHTML='获取中...'",
        :failure => "
          if(request.responseText == 'SSI_NEED_VERIFY') {
            alert('密码错误次数过多，需要验证');
          }
          if(request.responseText == 'SSI_NEED_VERIFY' || request.responseText == 'SSI_VERIFY_FAIL') {
            $('web_fetion_fetion_mobile').show();
            $('web_fetion_fetion_password').show();
            #{ remote_function( 
                :update => "fetion_verify_inputs",
                :url => verify_fetion_url(:default),
                :with => "'&mobile=' + $F('fetion_sm_#{tool_or_setting.id}_mobile')",
                :success => "$('fetion_verify').show()",
                :method => :get
              )}
          } else if( request.responseText == 'SSI_AUTH_FAIL' || request.responseText == 'cmcc_mobile_requested' || request.responseText == 'password_requested') {
            $('web_fetion_fetion_mobile').show();
            $('web_fetion_fetion_password').show();
            alert('获取好友失败，' + fetion_sm_state_name(request.responseText));
            $('fetion_friends_#{tool_or_setting.id}').innerHTML = '获取中...';
          } else if( request.responseText != 'SUCCESS') {
            alert('获取好友失败，' + fetion_sm_state_name(request.responseText));
            $('fetion_friends_#{tool_or_setting.id}').innerHTML = '获取中...';
          }
        ",
        :success => "
          Try.these(function() {
            $('fetion_verify').hide();
            $('verify_code').value = '';  
            $('verify_image_id').value = '';  
          }); "  + (has_logined_by_user? ? "
          #{ remote_function( :url => check_fetion_changed_user_url(current_user),
               :method => :get,
               :with => "'&mobile='+ $F('fetion_sm_#{tool_or_setting.id}_mobile') + '&password=' + $F('fetion_sm_#{tool_or_setting.id}_password')"
             ) 
           }          
          " : " "),
        :complete => "$('fetion_friends_#{tool_or_setting.id}').innerHTML='获取好友'"
      )}
    }",
    :class => :send_sm,
    :title => '获取好友',
    :class => 'all_friends',
    :id => "fetion_friends_#{tool_or_setting.id}"
  end
  
  def link_to_update_fetion_friends_in_show tool_or_setting, args = {}
    link_to_function "获取好友", "
      if($F('verify_code') == '请输入右侧图片中的验证码 =>') {
        alert('请输入验证码 =>');
      } else { #{
      remote_function(
        :update => args[:update],
        :url => friends_tools_web_fetions_url,
        :method => :get,
        :with => "'&mobile=' + $F('fetion_sm_#{tool_or_setting.id}_mobile')  +'&password=' + $F('fetion_sm_#{tool_or_setting.id}_password') + '&verifyCode=' + $F('verify_code') + '&verifyImgId=' + $F('verify_image_id')  ",
        :loading => "$('fetion_friends_#{tool_or_setting.id}').innerHTML='获取中...'",
        :failure => "
          if(request.responseText == 'SSI_NEED_VERIFY') {
            alert('密码错误次数过多，需要验证');
          }
          if(request.responseText == 'SSI_NEED_VERIFY' || request.responseText == 'SSI_VERIFY_FAIL') {
            $('web_fetion_fetion_mobile').show();
            $('web_fetion_fetion_password').show();
            #{ remote_function( 
                :update => "fetion_verify_inputs",
                :url => verify_fetion_url(:default),
                :with => "'&mobile=' + $F('fetion_sm_#{tool_or_setting.id}_mobile')",
                :success => "$('fetion_verify').show()",
                :method => :get
              )}
          } else if( request.responseText == 'SSI_AUTH_FAIL' || request.responseText == 'cmcc_mobile_requested' || request.responseText == 'password_requested') {
            $('web_fetion_fetion_mobile').show();
            $('web_fetion_fetion_password').show();
            alert('获取失败，' + fetion_sm_state_name(request.responseText));
            $('fetion_friends_#{tool_or_setting.id}').innerHTML = '获取中...';
          } else if( request.responseText != 'SUCCESS') {
            alert('获取失败，' + fetion_sm_state_name(request.responseText));
            $('fetion_friends_#{tool_or_setting.id}').innerHTML = '获取中...';
          }
        ",
        :success => "
          Try.these(function() {
            $('fetion_verify').hide();
            $('verify_code').value = '';  
            $('verify_image_id').value = '';  
          });" + (has_logined_by_user? ? "
          #{ remote_function( :url => check_fetion_changed_user_url(current_user),
               :method => :get,
               :with => "'&mobile='+ $F('fetion_sm_#{tool_or_setting.id}_mobile') + '&password=' + $F('fetion_sm_#{tool_or_setting.id}_password')"
             ) 
           }          
          " : ""),
        :complete => "$('fetion_friends_#{tool_or_setting.id}').innerHTML='获取好友'"
      )}
    }",
    :class => :send_sm,
    :title => '获取好友',
    :class => 'all_friends',
    :id => "fetion_friends_#{tool_or_setting.id}"
  end
end
